/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.service.impl;

import java.util.Date;

import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.purpose.bean.WorkListTypeBean;
import com.koron.purpose.domain.WorkListType;
import com.koron.purpose.mapper.WorkListTypeMapper;
import com.koron.purpose.service.api.WorkListTypeService;

@Service
public class WorkListTypeServiceImpl implements WorkListTypeService {

	@Override
	public MessageBean<?> insert(WorkListType bean, StaffBean user) {
		MessageBean<?> msg = MessageBean.create(0, "SUCCESS", void.class);
		if(StringUtils.isEmpty(bean.getWorkTypeId()) || StringUtils.isEmpty(bean.getWorkContent()) 
				|| StringUtils.isEmpty(bean.getCycleId())){
			msg.setCode(35001);
			msg.setDescription("参数校验异常");
			return msg;
		}
		try(SessionFactory factory = new SessionFactory();){
			bean.setAddTime(new Date());
			bean.setAddUser(user.getCode());
			bean.setAddUserName(user.getName());
			bean.setUpdateTime(new Date());
			bean.setUpdateUser(user.getCode());
			bean.setUpdateUserName(user.getName());
			bean.setId(DefaultIdGenerator.getInstance().generateLongId());
			bean.setState(1);
			factory.getMapper(WorkListTypeMapper.class).insert(bean);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常" + e.toString());
			return msg;
		}
		return msg;
	}

	@Override
	public MessageBean<?> update(WorkListType bean, StaffBean user) {
		MessageBean<?> msg = MessageBean.create(0, "SUCCESS", void.class);
		if(StringUtils.isEmpty(bean.getId()) || StringUtils.isEmpty(bean.getWorkTypeId()) || 
				StringUtils.isEmpty(bean.getWorkContent()) || StringUtils.isEmpty(bean.getCycleId())){
			msg.setCode(35001);
			msg.setDescription("参数校验异常");
			return msg;
		}
		try(SessionFactory factory = new SessionFactory();){
			bean.setUpdateTime(new Date());
			bean.setUpdateUser(user.getCode());
			bean.setUpdateUserName(user.getName());
			factory.getMapper(WorkListTypeMapper.class).update(bean);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常" + e.toString());
			return msg;
		}
		return msg;
	}

	@Override
	public MessageBean<?> delete(String id) {
		MessageBean<?> msg = MessageBean.create(0, "SUCCESS", void.class);
		if(StringUtils.isEmpty(id)){
			msg.setCode(35001);
			msg.setDescription("参数校验异常");
			return msg;
		}
		try(SessionFactory factory = new SessionFactory();){
			factory.getMapper(WorkListTypeMapper.class).delete(id);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常" + e.toString());
			return msg;
		}
		return msg;
	}

	@Override
	public MessageBean<?> list(WorkListTypeBean bean) {
		MessageBean<DataBean> msg= MessageBean.create(0, "success", DataBean.class);
		Integer totalPage = null;
	    Integer totalNumber = null;
	    Integer start = null;
	    Integer end = null;
	    Integer page=bean.getPage();
	    Integer pageSize=bean.getPageSize();
	    if (null!=page && pageSize!=null){
            if (page >0 && pageSize > 0){
            }else{
            	//第一页 10条数据
            	page =1;
            	pageSize=10;
            }
            start=(page<=1)?0:(page-1)*pageSize;
            end = pageSize;
        }
	    bean.setStart(start);
	    bean.setEnd(end);
	    try(SessionFactory factory = new SessionFactory();){
	    	totalNumber = factory.getMapper(WorkListTypeMapper.class).count(bean);
	    	if(start != null && end != null){
				totalPage = (totalNumber+pageSize-1)/pageSize;
			}else{
				totalPage = 1;
				page = totalPage;
				pageSize = totalNumber;
			}
	    	DataBean data = new DataBean();
			data.setList(factory.getMapper(WorkListTypeMapper.class).query(bean));
			data.setPageNo(page);
			data.setPageSize(pageSize);
			data.setTotalNumber(totalNumber);
			data.setTotalPage(totalPage);
			msg.setData(data);
	    } catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常" + e.toString());
		}
	    return msg;
	}

	@Override
	public MessageBean<?> details(String id) {
		MessageBean<WorkListType> msg = MessageBean.create(0, "SUCCESS", WorkListType.class);
		if(StringUtils.isEmpty(id)){
			msg.setCode(35001);
			msg.setDescription("参数校验异常");
			return msg;
		}
		try(SessionFactory factory = new SessionFactory();){
			WorkListType bean = factory.getMapper(WorkListTypeMapper.class).queryById(id);
			msg.setData(bean);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常" + e.toString());
			return msg;
		}
		return msg;
	}
	
	
}
